﻿using System.IO;
using KinonekoSoftware.CodariaDB.Presets;
using KinonekoSoftware.Studio.Models.Shells;

namespace KinonekoSoftware.Studio.Resources.Converters
{
    public class AvatarConverter : ImageConverterBase<GravatarSystem>
    {
        private BitmapImage _default;

        protected override ImageSource GetFallbackImage()
        {
            if (_default is null)
            {
                var    avatar  = GravatarSystem.GetPresetAvatarFilePath(Engine.Workspace);
                var    setting = Ioc.Get<JuXiaoYouSetting>();
                byte[] buffer;

                if (setting.Preset_Avatar == Preset_Avatar.Override)
                {
                    if (File.Exists(avatar))
                    {
                        buffer   = File.ReadAllBytes(avatar);
                        _default = MediaFactory.ToBitmap(buffer);
                        return _default;
                    }

                    setting.Preset_Avatar = Preset_Avatar.Avatar_4;
                }
                
                
                buffer   = GravatarSystem.GetPresetAvatar(setting.Preset_Avatar);
                _default = MediaFactory.ToBitmap(buffer);
            }

            return _default;
        }
    }
}